#include "MyGame.h"
#include "TestCube.h"

MyGame::MyGame(void)
{
	camera = new RCCamera(XMFLOAT3(0, 0, 0), XMFLOAT3(0, 1, 0), XMQuaternionIdentity());
	AddComponent(std::shared_ptr<IRCGameComponent>(camera));

	setTitle(L"RC Sand Box 3D");

	int cubes = 8;
	for (int c = 0; c < cubes; c++)
	{
		TestCube* testCube = new TestCube();

		XMFLOAT3 pos;
		XMFLOAT4 col;
		switch (c)
		{
		case 0:
			pos = XMFLOAT3(-1, 0, 1);
			col = XMFLOAT4(1, 1, 1, 1);
			break;
		case 1:
			pos = XMFLOAT3(1, 0, 1);
			col = XMFLOAT4(1, 0, 1, 1);
			break;
		case 2:
			pos = XMFLOAT3(-1, 0, 3);
			col = XMFLOAT4(1, 1, 0, 1);
			break;
		case 3:
			pos = XMFLOAT3(1, 0, 3);
			col = XMFLOAT4(0, 1, 1, 1);
			break;
		case 4:
			pos = XMFLOAT3(-1, 2, 1);
			col = XMFLOAT4(0, 0, 1, 1);
			break;
		case 5:
			pos = XMFLOAT3(1, 2, 1);
			col = XMFLOAT4(1, 0, 0, 1);
			break;
		case 6:
			pos = XMFLOAT3(-1, 2, 3);
			col = XMFLOAT4(0, 1, 0, 1);
			break;
		case 7:
			pos = XMFLOAT3(1, 2, 3);
			col = XMFLOAT4(.5, .5, .5, 1);
			break;
		}
		testCube->setCamera(camera);
		testCube->setPosition(pos);
		testCube->Color = col;

		AddComponent(std::shared_ptr<IRCGameComponent>(testCube));
	}
}


MyGame::~MyGame(void)
{
	camera = nullptr;
}

void MyGame::Initialize()
{
	RCGame::Initialize();	
}

void MyGame::Update(float time, std::vector<IUpdateable*>* updateables)
{
	HandleKeyboardInput();

	RCGame::Update(time, updateables);
}

void MyGame::Draw(float time, std::vector<IDrawable*>* drawables)
{
	clearColor = XMFLOAT4(.39, .58, .929, 1); // Cornflowerblue ;)
	GraphicsDevice.Clear(clearColor, D3D11_CLEAR_DEPTH, 1.0f, 0);

	RCGame::Draw(time, drawables);
}

void MyGame::HandleKeyboardInput()
{
	float spd = .05f;
	float rotSpd = .01f;

	float playerSpeed = 8.0f;

	if (KeyboardManager.getState()[DIK_ESCAPE] / 128)
		RCGame::Stop();

	if (KeyboardManager.getState()[DIK_LEFT] / 128)
		camera->Rotate(XMVectorSet(0, 1, 0, 0), -rotSpd);

	if (KeyboardManager.getState()[DIK_RIGHT] / 128)
		camera->Rotate(XMVectorSet(0, 1, 0, 0), rotSpd);

	if (KeyboardManager.getState()[DIK_UP] / 128)
		camera->Rotate(XMVectorSet(1, 0, 0, 0), -rotSpd);

	if (KeyboardManager.getState()[DIK_DOWN] / 128)
		camera->Rotate(XMVectorSet(1, 0, 0, 0), rotSpd);

	if (KeyboardManager.getState()[DIK_SPACE] / 128)
		camera->setOrientation(XMQuaternionIdentity());

	if (KeyboardManager.getState()[DIK_W] / 128)
		camera->Translate(XMFLOAT3(0, 0, spd));

	if (KeyboardManager.getState()[DIK_S] / 128)
		camera->Translate(XMFLOAT3(0, 0, -spd));

	if (KeyboardManager.getState()[DIK_A] / 128)
		camera->Translate(XMFLOAT3(-spd, 0, 0));

	if (KeyboardManager.getState()[DIK_D] / 128)
		camera->Translate(XMFLOAT3(spd, 0, 0));

}